<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 重新排列句子中的单词 -->
    <script>
      var arrangeWords = function (text) {
        let arr = text.split(' ').sort((a, b) => a.length - b.length)
        let newArr = arr.map((item, index) => {
          if (index === 0) {
            item = item.toLowerCase().replace(/( |^)[a-z]/g, L => L.toUpperCase())
          } else {
            item = item.toLocaleLowerCase()
          }
          return item
        })
        return newArr.join(' ')
      }
      console.log(arrangeWords('Leetcode is cool'))
    </script>
  </body>
</html>
